<?php
/**
 * 订单管理 2016-4-12
 */
namespace AdminV3\Controller;
use Think\Page;


class OrderController extends CommonController{

    public function index()
    {
    	
		$code = trim(I('get.code'));
		$code_status = trim(I('get.code_status'));
		
		
    	$o = D('Order');
        $od = M('Orderdetial');
		if($code_status==''||$code_status=='1'){
			$list=$o->select();
		}
		if($code_status=='2'||$code_status=='3'||$code_status=='4'||$code_status=='5'||$code_status=='6'){
			
			$list=$o->where('code_status='.$code_status)->select();
		}
		if($code!=''){
			
			$list=$o->where('code='.$code)->select();
		}
		
		
        $result = array(); 
        foreach ($list as $k => $v) {
        	$newresult = array();
            $tar = $v;         
            $odlist = $od->where(' order_id='.$v['id'])->select(); 
            $tar['orderdetial'] = $odlist;
            foreach($odlist as $x => $y){
            	$tar['orderdetial'][$y['jxs_id']][]=$y;				
            }
            $result[] = $tar;
        }
		$count=$o->count();
        $Page = new Page($count,5);
        foreach ($result as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $result);
        $this->display();

    }

	function delivery(){
		$id = I('id'); 
		$data = array(
			'wuliu_code'	=>	I('wuliu_code'),
			'wuliu_company'	=>	I('wuliu_company'),
			'wuliu_status'	=>	3,
			'code_status'	=>	4,
			'wuliu_time' =>time(),
			
		);
		if(empty($id)){
			$this->_ajaxreturn(false,'缺少必要参数');
		}
		if(empty($data['wuliu_code'])||empty($data['wuliu_company'])){
			$this->_ajaxreturn(false,'不能为空');
		}
		$Model = D('Order');
		$rs = $Model->where('id='.$id)->save($data); 
		if($rs){
			$this->_ajaxreturn(true,'修改完成');
		}else{
			$this->_ajaxreturn(false,'修改失败，请稍后重试');
		}
	}
	
		
	function uplivery(){
		$id = I('id'); 
		$data = array(
			'r_username'	=>	I('r_username'),
			'r_phone'	=>	I('r_phone'),
			'r_addr'	=>	I('r_addr'),
			
			
		);
		if(empty($id)){
			$this->_ajaxreturn(false,'缺少必要参数');
		}
		if(empty($data['r_username'])||empty($data['r_phone'])||empty($data['r_addr'])){
			$this->_ajaxreturn(false,'不能为空');
		}
		$Model = D('Order');
		$rs = $Model->where('id='.$id)->save($data); 
		if($rs){
			$this->_ajaxreturn(true,'修改完成');
		}else{
			$this->_ajaxreturn(false,'修改失败，请稍后重试');
		}
	}
	
	
	function refund(){
		$id = I('id'); 
		$data = array(
			'r_username'	=>	I('r_username'),
			'r_phone'	=>	I('r_phone'),
			'r_addr'	=>	I('r_addr'),
			
			
		);
		if(empty($id)){
			$this->_ajaxreturn(false,'缺少必要参数');
		}
		if(empty($data['r_username'])||empty($data['r_phone'])||empty($data['r_addr'])){
			$this->_ajaxreturn(false,'不能为空');
		}
		$Model = D('Order');
		$rs = $Model->where('id='.$id)->save($data); 
		if($rs){
			$this->_ajaxreturn(true,'修改完成');
		}else{
			$this->_ajaxreturn(false,'修改失败，请稍后重试');
		}
	}
		
	
	function _ajaxreturn($s=true,$m='Succes',$d=array()){
		exit(json_encode(array('s'=>$s,'m'=>$m,'d'=>$d)));
	}


 

  public function alter()
    {
        $idList = I('post.idlist');
        $type = I('post.type');
        $result = array();
        if (!empty($type)) {
            if (!empty($idList)) {
                $idList = rtrim($idList, ',');
                //var_dump($idList);
                $idList = explode(',', $idList);
                //var_dump($idList);
                $data['ok'] = 0;
                $data['error'] = 0;

                foreach ($idList as $k => $val) {
                    //通过审核
                    switch ($type) {
                        
                        
						case 'restore':
                            $newType = 1;
                            break;
                        case 'cancel':
                            $newType = 2;
                            break;
                        case 'del':
                            $newType = 3;
                            break;
                    }

                    $re = M('Order')->where('id=' . $val)->setField('status', $newType);
                    if ($re > 0) {
                        $data['ok']++;
                    } else {
                        $data['error']++;
                    }
                }
                $result['mes'] = "操作成功:{$data['ok']},失败{$data['error']}";
                $this->ajaxReturn($result);

            } else {
                $result['mes'] = '没有选择的值';
                $this->ajaxReturn($result);
            }
        } else {
            $result['mes'] = '非法操作!';
            $this->ajaxReturn($result);
        }

    }   
    public function delList()
    {

    	$o = D('Order');		
        $od = M('Orderdetial');
       

        $list = $o->select(); 
        
        $result = array(); 
        foreach ($list as $k => $v) {
            $tar = $v;            
            $odlist = $od->where(' order_id='.$v['id'])->select(); 
            $tar['orderdetial'] = $odlist;//设键值
            $result[] = $tar;
           
        }
        $this->assign('list', $result); 
        $this->display();




    }

}